/**
 * @Title: DeviceServiceImpl.java
 * @Package me.springboot.mybatis.service.impl
 * @Description: TODO
 * @author zengh
 * @date 2018/04/15
 * @version V1.0
 */
package me.springboot.mybatis.service.impl;

import me.springboot.mybatis.core.AbstractService;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.dao.DeviceMapper;
import me.springboot.mybatis.dao.XunJianDeviceMapper;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Device;
import me.springboot.mybatis.model.XunJianDevice;
import me.springboot.mybatis.service.DeviceService;
import me.springboot.mybatis.web.req.DeviceReq;
import org.springframework.objenesis.ObjenesisBase;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zengh
 * @ClassName: DeviceService
 * @Description: TODO
 * @date 2018/04/15
 */
@Service
@Transactional
public class DeviceServiceImpl extends AbstractService<Device> implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private XunJianDeviceMapper xunJianDeviceMapper;

    @Override
    public void addDevice(Device device) throws ResultException {
        /*Device temp = findBy("num", device.getNum());
        if (null != temp) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "编号已存在！");
        }*/
        Device temp = findBy("equipmentNum", device.getEquipmentNum());
        if (null != temp) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "设备号已存在！");
        }

        List<Device> devices = deviceMapper.lastRecord(device);
        if (null != devices && devices.size() == 1) {
            device.setGroups(devices.get(0).getGroups());
        }
        save(device);
    }

    @Override
    public List<Map<String, Object>> searchDevice(DeviceReq deviceReq) {
        return deviceMapper.searchDevice(deviceReq);
    }

    @Override
    public List<Map<String, Object>> searchXunJianDevice(DeviceReq deviceReq) {
        return deviceMapper.searchXunJianDevice(deviceReq);
    }

    @Override
    public List<Device> selectByModel(Device device) {
        return deviceMapper.selectByModel(device);
    }

    @Override
    public Map<String, Object> getCount(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = deviceMapper.getCount(paramMap);
        Map<String, Object> resultMap = new HashMap();
        for (Map<String, Object> map : list) {
            if (map.get("devicetype").toString().equals("1")) {//井盖控制器
                resultMap.put("cover", map.get("count"));
            } else {
                resultMap.put("trash", map.get("count"));
            }
        }
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> paiDanList(DeviceReq deviceReq) {
        return deviceMapper.paiDanList(deviceReq);
    }

    @Override
    public List<Device> notFenPeiDeviceList(DeviceReq deviceReq) {
        return deviceMapper.notFenPeiDeviceList(deviceReq);
    }

    @Override
    public void fenPeiXunJianDevice(String devices, String userid) {
        String[] device = devices.split(",");
        for (String s : device) {
            XunJianDevice xunJianDevice = new XunJianDevice();
            xunJianDevice.setDevicenum(s);
            xunJianDevice.setXunjianUserid(Integer.valueOf(userid));
            xunJianDevice.setIntime(new Date());
            xunJianDeviceMapper.insert(xunJianDevice);
        }
    }
}